<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>凭证管理--分配账号</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/static/plugin/layui/css/layui.css}" media="all">
</head>
<body>
<div class="searchTable">
    <div class="layui-inline">
        <input class="layui-input" name="s_realname" id="s_realname" autocomplete="off" placeholder="昵称">
    </div>
    <div class="layui-inline">
        <input class="layui-input" name="s_username" id="s_username" autocomplete="off" placeholder="账号">
    </div>
    <button type="button" id="searchBtn" class="layui-btn" data-type="reload">搜索</button>
</div>

<!--二维列表-->
<table class="layui-hide" id="AssignAccountTable" lay-filter="AssignAccountTable"></table>

<input type="text" id="ctx" hidden="hidden" th:value="${#request.getContextPath()}">
<input type="hidden" th:value="${voucherId}" id="voucherId">

<!--新增表单-->
<div class="layui-row" id="addForm" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" action="" style="margin-top:20px; margin-right: 20px">
            <div class="layui-form-item">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                    <input type="text" id="username" name="username" required lay-verify="required" autocomplete="off"
                           placeholder="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" style="margin-top:40px">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="confirm">新增</button>
                    <button type="button" class="layui-btn layui-btn-primary" id="back">取消</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/html" id="userToolBar">
    <div class="layui-btn-container">
        <!--shiro:hasPermission="/manageVoucher/assignAccountAdd"-->
        <button type="button" class="layui-btn layui-btn-sm" lay-event="add" >新增关联账号</button>
    </div>
</script>

<script type="text/html" id="tbBar">
    <!--删除关联账号-->
    <!--shiro:hasPermission="/manageVoucher/assignAccountDelete"-->
    <i style="font-size: 24px; color: #f4577b;" class="layui-icon layui-icon-delete " lay-event="remove"></i>
</script>

<script th:src="@{/static/js/jquery.min.js}" charset="utf-8"></script>
<script th:src="@{/static/plugin/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/static/js/app.js}" charset="utf-8"></script>
<script src="/static/js/whysu.js" th:src="@{/static/js/whysu.js}"></script>

<!--序号自增-->
<script type="text/html" id="xuhao">
    {{d.LAY_TABLE_INDEX+1}}
</script>

<script>
    layui.use(['jquery', 'table'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var voucherId = $('#voucherId').val();

        table.render({
            elem: '#AssignAccountTable'
            , url: ctx + '/manageVoucher/getAssignAccountPage'
            , method: 'post'
            , contentType: 'application/json'
            , toolbar: '#userToolBar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: false //去掉右上角的打印等按钮
            , title: 'AssignAccountTable'
            , loading: true //显示加载条
            , cols: [
                [
                    {title: '序号', templet: '#xuhao', align: 'center'}
                    /*凭证组-账号关联关系id*/
                    , {field: 'relativeId', title: '关联关系ID', align: 'center', hide: true}
                    /*账号id*/
                    , {field: 'accountId', title: '账号ID', align: 'center', hide: true}
                    , {field: 'realname', title: '昵称', align: 'center'}
                    , {field: 'username', title: '账号', align: 'center'}
                    , {field: 'roleId', title: '角色id', align: 'center', hide: true}
                    , {field: 'roleName', title: '角色', align: 'center'}
                    , {field: 'createBy', title: '创建者', align: 'center'}
                    ,{
                        field: 'createTime', title: '创建时间', align: 'center',
                        templet: function (d) {
                            if (d.createTime == null) {
                                return "";
                            } else {
                                return layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss');
                            }
                        }
                    },
                    {fixed: 'right', title: '操作', toolbar: '#tbBar'}
                ]
            ]
            , page: true
            , where: {
                "voucherId": voucherId
            }
            , request: {
                pageName: 'pageNum'//页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.body.totalSize, //解析数据长度
                    "data": res.body.list //解析数据列表
                };
            }

        });

        /*重载*/
        var $ = layui.$, active = {
            reload: function () {
                var queryWhere = {};
                // 要带上凭证组id
                queryWhere['voucherId'] = voucherId;
                // 账号
                var userName$_ = $('#s_username');
                if (userName$_.val() != '') {
                    queryWhere['queryUsername'] = userName$_.val();
                }
                // 昵称
                var realname$_ = $('#s_realname');
                if (realname$_.val() != '') {
                    queryWhere['queryRealname'] = realname$_.val();
                }
                //执行重载
                table.reload('AssignAccountTable', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: queryWhere
                    , done: function (res, curr, count) {
                        this.where = {};
                    }
                }, 'data');
            }
        };

        /*搜索*/
        $('#searchBtn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        /*新增关联账号*/
        form.on('submit(confirm)', function (massage) {
            // 参数
            var param = {};
            param.voucherId = voucherId;
            param.username = $("#username").val();
            $.ajax({
                url: ctx + '/manageVoucher/assignAccountAdd',
                type: "POST",
                contentType: "application/json",
                dataType: "json",
                data: requestDTO(param),
                beforeSend: function () {
                    this.layerLogin = showLoad();
                },
                complete: function () {
                    closeLoad(this.layerLogin);
                },
                success: function (data) {
                    if (data.result.code == 200) {
                        // 关闭所有弹窗
                        layer.closeAll();
                        // 操作成功
                        layer.msg("操作成功", {
                            time: 1000
                        });
                        // 重新加载表格
                        table.reload('AssignAccountTable');
                    } else {
                        layer.msg(data.result.message);
                    }
                },
                error: function (e) {
                    layer.msg("请求异常")
                }
            });
        });

        //头工具栏事件
        table.on('toolbar(AssignAccountTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                //自定义头工具栏右侧图标 - 提示
                case 'add': {
                    var updateIndex = layer.open({
                        //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type: 1,
                        title: "新增关联账号",
                        area: ['40%', '60%'],
                        content: $("#addForm")//引用的弹出层的页面层的方式加载修改界面表单
                    });
                    $("#username").val('');
                    break;
                }
                default:
                    break;
            }
        });

        //监听弹出框表单提交，massage是修改界面的表单数据'submit(confirm),是修改按钮的绑定

        //监听行工具事件
        table.on('tool(AssignAccountTable)', function (obj) {
            var data = obj.data;
            // console.log(obj)
            if (obj.event === 'remove') {
                layer.confirm('确定删除该关联账号？', function (index) {
                    $.ajax({
                        type: "POST",
                        url: ctx + "/manageVoucher/assignAccountDelete",
                        contentType: "application/json",
                        dataType: "json",
                        data: requestDTO(data.relativeId),
                        beforeSend: function () {
                            this.layerLogin = showLoad();
                        },
                        complete: function () {
                            closeLoad(this.layerLogin);
                        },
                        success: function (response) {
                            if (response.result.code == 200) {
                                layer.msg("操作成功", {
                                    timeout: 1000
                                }, function () {
                                    // 关闭窗口
                                    layer.close(index);
                                });
                                //执行重载
                                table.reload('AssignAccountTable');
                                return true;
                            } else {
                                layer.msg(response.result.message)
                            }
                        },
                        error: function (e) {
                            layer.msg("请求异常")
                        }
                    });
                });
            }
        });

        $("#back").on('click', function () {
            layer.closeAll();
        });

    });
</script>

</body>
</html>